﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace GUIL
{
    public partial class LoginGUI : Form, RemoteService.IRemoteServiceCallback
    {
        RemoteService.IRemoteService _remote;
        IEnumerable<RemoteService.Cinema> _cinemas;
        
        public RemoteService.Cinema selectedCinema { get; private set; }

        public LoginGUI()
        {
            // Hosting client interface ... (more info later)
            // Connection hasn't been opened yet!
            _remote = new RemoteService.RemoteServiceClient(new InstanceContext(this), "WSDualHttpBinding_IRemoteService");

            InitializeComponent();
        }

        private void LoginGUI_Load(object sender, EventArgs e)
        {
            try
            {
                _cinemas = _remote.GetCinemas();

                // create dictionary
                var dict = _cinemas.Select(c => new { c.Id, desc = c.Name + ", " + c.Location }).ToDictionary(c => c.Id, c => c.desc);

                // put data from dictionary in combobox
                comboBoxCinema.DataSource = new BindingSource(dict, null);
                comboBoxCinema.DisplayMember = "Value";
                comboBoxCinema.ValueMember = "Key";
            }
            catch (Exception)
            {
                MessageBox.Show("Failed to load login, try again");
                // exit application with error code
                Environment.Exit(1);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            LoginUser();
        }

        private void txtBoxInitials_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) // Call login function
            {
                LoginUser();
            }
        }

        /// <summary>
        /// Checks if user have rigths to log in, and then sets cinema if access is granted
        /// </summary>
        private void LoginUser()
        {
            if (txtBoxInitials.Text.Length > 0 && comboBoxCinema.SelectedItem != null)
            {
                // check if username exists in DB
                if (_remote.IsEmployeeAllowed(txtBoxInitials.Text))
                {
                    try
                    {
                        // get selected cinema object from dropdown dictionary
                        selectedCinema = _cinemas.Where(c => c.Id == ((KeyValuePair<int, string>)comboBoxCinema.SelectedItem).Key).Single();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    this.DialogResult = DialogResult.OK; // login success
                    this.Close();
                }
                else
                {
                    MessageBox.Show("User does not exist!");
                }
            }
            else
            {
                MessageBox.Show("No initials was entered and/or no cinema was chosen!");
            }
        }

        public void OnAction(List<RemoteService.ScreenCast_Seat> screenSeats)
        {
            throw new NotImplementedException();
        }
    }
}